# -*- coding: utf-8 -*-
__author__ = 'sunnychou'
__date__ = '2019/7/28 13:15'
#此文件作废，已经做过了

from common.orm_helper import *
from common.excel_helper import ExcelReader
from k3_data_report.import_tb.utils import fetch_tb_unique_his_data
from  common.logger_helper import  g_wlogger

##########################################
# 3 导入k3基础表  收款单  BEGIN
##########################################
class SoukuanList(Base):
    __tablename__ = "k3_shoukuandan"  # 自定义数据库表名称
    id = Column(Integer, primary_key=True)  # 自增
    sjsj = Column(String(10), verbose_name='年月')
    pay_type = Column(String(20), verbose_name='支付类型')
    status =  Column(String(20), verbose_name='状态')
    danju_code =  Column(String(20), verbose_name='单据号')
    danju_date =  Column(String(20), verbose_name='单据日期')
    caiwu_date =  Column(String(20), verbose_name='财务日期')
    hsxm_type =  Column(String(20), verbose_name='核算项目类别')
    kh_code =  Column(String(20), verbose_name='客户代码')
    kh_name =  Column(String(100), verbose_name='客户')
    bm_code =  Column(String(20), verbose_name='部门代码')
    bm_name =  Column(String(20), verbose_name='部门')
    yewu_code =  Column(String(20), verbose_name='业务员代码')
    yewu_name =  Column(String(20), verbose_name='业务员')
    bibie = Column(String(20), verbose_name='币别')
    danju_jine = Column(String(20), verbose_name='单据金额')
    shenhe_name = Column(String(20), verbose_name='审核人')
    year = Column(String(20), verbose_name='年份')
    shishou_jine = Column(String(20), verbose_name='实收金额')
    jiesuan_type = Column(String(100), verbose_name='结算方式')



def shoukuandan_process(excel_name, sheet_name):
    '''
    收款单处理过程
    :param excel_name:
    :param sheet_name:
    :return:
    '''
    ereader = ExcelReader(excel_name,sheet_name=sheet_name)
    headers = ereader.get_excel_headers()
    data_lists = ereader.get_excel_data_list()
    danju_date = data_lists[0][3]
    danju_month = "%s-%s" % (danju_date.split("-")[0], danju_date.split("-")[1])

    unique_select_sql = "select  distinct sjsj  from k3_shoukuandan"
    unique_set = fetch_tb_unique_his_data(unique_select_sql)
    if danju_month in unique_set:
        already_info = f"shoukuandan_process month:{danju_month} already existed."
        g_wlogger.winfo(already_info)
        return

    for data_list in data_lists:
        danju_code = data_list[2]
        if danju_code == "":
            continue
        shoukuan = SoukuanList(sjsj = danju_month, pay_type=data_list[0],status=data_list[1],danju_code=data_list[2],
                        danju_date=data_list[3], caiwu_date=data_list[4], hsxm_type=data_list[5],
                        kh_code=data_list[6],  kh_name=data_list[7], bm_code=data_list[8],
                        bm_name=data_list[9],yewu_code=data_list[10],yewu_name=data_list[11],
                        bibie=data_list[12], danju_jine=data_list[13],shenhe_name=data_list[14],
                        year=data_list[15],shishou_jine=data_list[16], jiesuan_type=data_list[17]
                        )
        session.add(shoukuan)
        session.commit()

    skd_succ = f"收款单 文件：{excel_name}， 日期：{danju_month}处理完毕！"
    g_wlogger.winfo(skd_succ)



def shoukuandan_process_main(excel_name, sheet_name):
    try:
        shoukuandan_process(excel_name, sheet_name)
        return True
    except Exception as e:
        g_wlogger.werror(f"shoukuandan_process_main has error:{e} with excel:{excel_name}")
        return False
##########################################
# 1 导入k3基础表  收款单   END
##########################################



